<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\admin\Logic;
use app\admin\model\BottleSite;
use app\admin\model\Bottle;
use app\common\server\UrlServer;
use think\Db;


class BottleSiteLogic{
    public static function lists($get){
        $where[] = ['del','=',0];
        //类型搜索
        if (isset($get['type']) && $get['type']) {
            $where[] = ['type', '=' , $get['type']];
        }

 
        if (isset($get['head_name']) && $get['head_name']) {
            $where[] = ['head_name', '=' , $get['head_name']];
        }

        if (isset($get['head_mobile']) && $get['head_mobile']) {
            $where[] = ['head_mobile', '=' , $get['head_mobile']];
        }

        if (isset($get['empty_bottle_num']) && $get['empty_bottle_num']) {
            $where[] = ['empty_bottle_num', '=' , $get['empty_bottle_num']];
        }

        if (isset($get['solid_bottle_num']) && $get['solid_bottle_num']) {
            $where[] = ['solid_bottle_num', '=' , $get['solid_bottle_num']];
        }
        //启用日期
        if (isset($get['start_time']) && $get['start_time'] != '') {
            $where[] = ['createtime', '>=', $get['start_time']];
        }
        if (isset($get['end_time']) && $get['end_time'] != '') {
            $where[] = ['createtime', '<=', $get['end_time']];
        }
        
        $bottle_site = new BottleSite();
        $bottle = new Bottle();
        $bottle_site_count = $bottle_site
            ->where($where)
            ->count();
        $field = 'bs.*';
        $bottle_site_list = $bottle_site
            ->alias('bs')
            ->where($where)
            ->field($field)
            ->page($get['page'], $get['limit'])
            ->order('id desc')
            ->select()->toArray();;
        foreach($bottle_site_list as $key=>$val){
            // 获取钢瓶不同状态
            $bottle_site_list[$key]['solid_bottle_num'] = $bottle->where(["site_id"=>$val["id"],"status"=>Bottle::STATUS1])->count("id");//实瓶
            $bottle_site_list[$key]['empty_bottle_num'] = $bottle->where(["site_id"=>$val["id"],"status"=>Bottle::STATUS2])->count("id");//空瓶
        }
//        $bottle_site_list->append(['createtime_desc']);
//        var_dump($bottle_site_list);
        return ['count' => $bottle_site_count, 'list' => $bottle_site_list];

    }

    //添加钢瓶
    public static function add($post){
        $bottle_site = new BottleSite();
        //拼接数据
        $data = [
            'name'             => $post['name'],
            'head_name'   => $post['head_name'],
            'head_mobile'     => $post['head_mobile'],
            'type'   => $post['type'],
            'createtime'        => date("Y-m-d H:i:s",time()),
            'address' =>  $post['address'],
            'latitude'  =>  $post['latitude'],
            'latitude2'  =>  $post['latitude2'],
            'delivery_range_meter'  =>  $post['delivery_range_meter'],

        ];

        return $bottle_site->allowField(true)->save($data);

    }

    public static function edit($post){

        $bottle_site = new BottleSite();
        //拼接数据
        $data = [
            'name'             => $post['name'],
            'head_name'   => $post['head_name'],
            'head_mobile'     => $post['head_mobile'],
            'type'   => $post['type'],
            'address' =>  $post['address'],
            'latitude'  =>  $post['latitude'],
            'latitude2'  =>  $post['latitude2'],
            'delivery_range_meter'  =>  $post['delivery_range_meter'],
        ];
        

        return $bottle_site->allowField(true)->save($data, ['id' => $post['id']]);


    }

    public static function getBottleSite($id,$get_data = false){
        $bottle_site = BottleSite::get($id);
        return $bottle_site;
    }



    /*
     * 删除站点
     */
    public static function del($id){
        $data = [
            'del' => 1,
        ];
        return Db::name('bottle_site')->where(['del' => 0, 'id' => $id])->update($data);
    }

    /*
     * 发放记录
     */
    public static function log($get){
        $where[] = ['cl.del','=',0];
        $where[] = ['coupon_id','=',$get['id']];
        if(isset($get['keyword']) && $get['keyword']){
            $where[] = ['nickname|mobile','like','%'.$get['keyword'].'%'];
        }

        $log_count = Db::name('coupon_list cl')
            ->join('user u','cl.user_id = u.id')
            ->where($where)
            ->count();

        $log_list =  Db::name('coupon_list cl')
            ->join('user u','cl.user_id = u.id')
            ->where($where)
            ->field('coupon_id,status,coupon_code,cl.create_time as create_time,cl.use_time,u.nickname,u.avatar,u.mobile,u.sex,u.sn,u.level,u.create_time as u_create_time')
            ->page($get['page'], $get['limit'])
            ->select();
        $coupon_list = Db::name('coupon')->where(['del'=>0])->column('name','id');
        $level_name = Db::name('user_level')->where(['del'=>0])->column('name','id');
        foreach ($log_list as &$item)
        {
            $item['use_time_desc'] = '-';
            $item['status_desc'] = '未使用';
            $item['create_time_desc'] = '';
            $item['sex_desc'] = '未知';
            if($item['use_time']){
                $item['use_time_desc'] = date('Y-m-d H:i:s',$item['use_time']);
            }
            $item['u_create_time'] = date('Y-m-d H:i:s',$item['u_create_time']);

            if ($item['status'] == 1) {
                $item['status_desc'] = '已使用';
            } else if($item['status'] == 2) {
                $item['status_desc'] = '已过期';
            }

            $item['avatar'] = UrlServer::getFileUrl($item['avatar']);
            $item['coupon_name'] = $coupon_list[$item['coupon_id']] ?? '';
            $item['create_time_desc'] = date('Y-m-d H:i:s',$item['create_time']);

            $item['level_name'] = $level_name[$item['level']] ?? '';
            switch ($item['sex']){
                case 1:
                    $item['sex_desc'] = '男';
                case 2:
                    $item['sex_desc'] = '女';

            }

        }
        return ['count'=>$log_count , 'lists'=>$log_list];
    }

    /*
     * 关闭优惠券
     */
    public static function close($id){
        $coupon = new Coupon();
        return $coupon->save(['status'=>0,'update_time'=>time()],['id'=>$id]);
    }


    public static function sendCoupon($post){
        $user_ids = $post['user_ids'];
        $coupon_ids = $post['coupon_ids'];
        $now = time();
        foreach ($coupon_ids as $coupon_id){
            foreach ($user_ids  as $user_id){
                $add_data = [
                    'user_id'       => $user_id,
                    'coupon_id'     => $coupon_id,
                    'status'        => 0,
                    'coupon_code'   => create_coupon_code(),
                    'create_time'   => $now,
                ];
                Db::name('coupon_list')->insert($add_data);

            }
        }
        return true;

    }




}